home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / Tool Chest / Devices & Hardware / A⁄ROSE 1.2 / Downloader / ndld.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-10  |  12.0 KB  |  486 lines  |  [TEXT/MPS ]

  1. /*
  2.  
  3.     Program to test Code download subroutine
  4.  
  5.     Written by:        Anumele D. Raja
  6.  
  7.     Date:            May 6, 1988
  8.     
  9.     Last modified:    April 10, 1989
  10.  
  11. */
  12.  
  13. /*  Main includes  */
  14.  
  15. #include <Types.h>                /*  Nearly always required  */
  16. #include <Quickdraw.h>            /*  to access the qd globals  */
  17. #include <Fonts.h>                /*    for InitFonts() and possible use of fonts  */
  18. #include <Resources.h>            /*  resource file calls  */
  19. #include <Events.h>                /*  very important for multi-tasking under multi finder  */
  20. #include <Windows.h>            /*  window manager calls  */
  21. #include <Controls.h>            /*  control manager calls  */
  22. #include <Dialogs.h>            /*  dialog manager calls  */
  23. #include <Menus.h>                /*  menu manager calls  */
  24. #include <TextEdit.h>            /*  text editor  */
  25. #include <Packages.h>            /*  standard file package  */
  26. #include <Strings.h>            /*  include for debugger  */
  27. #include <Stdio.h>                /*  standard I/O routines  */
  28. #include <Files.h>                /*  file manager equates  */
  29. #include <OSEvents.h>            /*  OS events  */
  30. #include <SysEqu.h>                /*  Sys equates for CurDirStore  */
  31. #include <ToolUtils.h>            /*  Tools utilities like LoWord/HiWord  */
  32.  
  33. #include "os.h"
  34. #include "arose.h"
  35.  
  36. #include "siop.h"
  37. #include "download.h"
  38.  
  39. Point inpLocn = {50, 50};
  40. SFTypeList typeList = {'TEXT'};
  41.  
  42. #define    NIL    0
  43. #define    TRUE 0xff
  44. #define    FALSE 0x00
  45. #define firstTime -1
  46. #define nullEvent 100
  47. #define DLOG_ERR 130
  48. #define continueButton 1
  49.  
  50. #define NoOfItems 21
  51.  
  52. short option, volRefNum;
  53. short slotNUM;
  54. short slotMask;
  55. Boolean slotFlag[6];    /* maximum number of downloadable slots is 6 */
  56. short slotGood[6];
  57. char hiliteDownload;
  58. char rsrcPut;
  59. short errCode;
  60. SFReply reply;
  61.  
  62. char resTypeStr[5]="CODE";
  63. char PrevRsrc[5] = "CODE";
  64. Boolean Cancel = FALSE;
  65.  
  66. DialogPtr mainDlg;
  67.  
  68. pascal short myDlgHook(short, DialogPtr);
  69. void SetupResource(DialogPtr, char *);
  70.  
  71. long cnvhex();
  72. void startMCPHW();
  73. void startASTHW();
  74.  
  75. void stopMCPHW();
  76. void stopASTHW();
  77.  
  78. pascal void ProgProc(long segSize);
  79.  
  80. unsigned long AST_FindRAMLength();
  81. unsigned long MCP_FindRAMLength();
  82.  
  83. /*  Error messages  */
  84.  
  85. char errMsg[][32] = {
  86.     "\pNo Error", /* 0 */
  87.     "\pNo Jump Table Found", /* 1 */
  88.     "\pBad Data Init Segment", /* 2 */
  89.     "\pGlobal Data Format Error", /* 3 */
  90.     "\pCode Segment Error", /* 4 */
  91.     "\pCan run only on Mac II", /* 5 */
  92.     "\pInvalid Slot", /* 6 */
  93.     "\pSlot specified is empty", /* 7 */
  94.     "\pCould not open Resource File", /* 8 */
  95.     "\pDownload file is wrong type", /* 9 */
  96.     "\pStarting error", /* 10 */
  97.     "\pNo memory", /* 11 */
  98.     "\pRSM Error", /* 12 */
  99.     "\pNo RSM", /* 13 */
  100.     "\pNo A/ROSE running on slot", /* 14 */
  101.     "\pNo 'CNFG' resource", /* 15 */
  102.     "\pNo A/ROSE Prep File", /* 16 */
  103.     "\pYou are exiting download", /* 17 */
  104.     "\pNo slot selected for download", /* 18 */
  105.     "\pDownloading is successful" /* 19 */
  106. };
  107.  
  108. main()
  109. {
  110.     struct ST_Registers regs;
  111.     short refNum, slotSel, slot, i;
  112.     Handle dlogRes;
  113.     DialogTPtr dlogPtr;
  114.     short status;
  115.  
  116.     /* Initialize managers */
  117.     
  118.     InitGraf(&qd.thePort);
  119.     InitFonts();
  120.     FlushEvents(everyEvent, 0);
  121.     InitWindows();
  122.     InitMenus();
  123.     InitCursor();
  124.  
  125.     TEInit();
  126.     InitDialogs(NULL);
  127.     
  128.     NewFindcard(&slotMask, 0);
  129.  
  130.     dlogRes = GetResource('DLOG', 128);    /* update resource with refNum */
  131.     dlogPtr = (DialogTPtr)*dlogRes;
  132.     *(long *)CurDirStore = dlogPtr->refCon;
  133.     ChangedResource(dlogRes);
  134.     
  135.     errCode = 0;
  136.  
  137.     for (i=0; i<6; i++) {        /* set flags for check boxes  */
  138.         slotFlag[i] = FALSE;
  139.     }
  140.  
  141.     slotSel = 0;
  142.     option = 0;
  143.     
  144.     while (!Cancel) {
  145.         hiliteDownload = FALSE;
  146.         rsrcPut = FALSE;
  147.  
  148.         sfpgetfile(&inpLocn, "File Name:", NIL, -1, typeList, myDlgHook, &reply, 128, NIL);
  149.         if (!Cancel) {
  150.  
  151.             if ((refNum = OpenRFPerm(reply.fName, reply.vRefNum, fsRdPerm)) != -1) {
  152.                 slotSel = 0;
  153.                 for (i=0; i<6; i++) {
  154.                     if (slotFlag[i])
  155.                         slotSel |= 1<<(i+9);
  156.                 }
  157.                 status = 18;    /*  Say no card is selected  */
  158.                 for (slot=0; slot<16; slot++) {
  159.                     if ((1<<slot) & slotSel) {
  160.                         slotNUM = slot;
  161.                         if (!option) {
  162.                             HaltCard(slotNUM);
  163.                             status = NewDownload(slotNUM, DEF_ADDROFFSET, DEF_LOADADDR, (struct gCommon *)0x400,
  164.                                  option?0:DL_INITLOAD|DL_CLEARMEM,
  165.                                  (ResType)*((long *)resTypeStr), ®s, 0);
  166.                             StartCard(slotNUM);
  167.                         }
  168.                         else {
  169.                             status = NewDownload(slotNUM, 0, 0, (struct gCommon *)0x400, 0, (ResType)*((long *)resTypeStr), ®s, 0); 
  170.                         }
  171.                     }
  172.                 }
  173.                 CloseResFile(refNum);
  174.                 
  175.                 dlogRes = GetResource('DLOG', 128);    /* update resource with refNum */
  176.                 dlogPtr = (DialogTPtr)*dlogRes;
  177.                 dlogPtr->refCon = *(long *)CurDirStore;
  178.                 ChangedResource(dlogRes);
  179.             }
  180.             else
  181.                 status = DLE_FILEWRONG;
  182.         }
  183.         else
  184.             status = DLE_ABORT;
  185.     
  186.         if (status != DLE_NOERR) {
  187.             errCode = status;
  188.         }
  189.         else {
  190.             errCode = 19;
  191.         }
  192.     }
  193. }
  194.  
  195. struct itemTbl {
  196.     short itemNo;
  197.     struct radItem *itmPtr;
  198. };
  199.  
  200. struct radItem {
  201.     short riValue;
  202.     struct radDesc *radPtr;
  203. };
  204.  
  205. struct radDesc {
  206.     short *radValPtr;
  207.     short radNoItems;
  208.     short *radItems;
  209. };
  210.  
  211. short radListOption[] = { 11, 12 };
  212.  
  213. struct radDesc radOption = { &option, sizeof(radListOption)/sizeof(short), radListOption };
  214.  
  215. struct radItem initLoad = { 0, &radOption };
  216. struct radItem dynLoad = { 1, &radOption };
  217.  
  218. struct itemTbl itemTbl[] = {
  219.     {11, &initLoad}, {12, &dynLoad},
  220.     {13, &slotFlag[0]}, {14, &slotFlag[1]}, {15, &slotFlag[2]}, {16, &slotFlag[3]},
  221.     {17, &slotFlag[4]}, {18, &slotFlag[5]}, {23, &resTypeStr}
  222. };
  223. short cardItem[6] = { 13, 14, 15, 16, 17, 18 };
  224.  
  225. pascal short myDlgHook(item, myDialog)
  226. short item;
  227. DialogPtr myDialog;
  228. {
  229.     short itemType, i, j, menuID, noOfRsrcTypes, codeNum, refNum;
  230.     long result;
  231.     Handle control;
  232.     Rect box, tempRect, popUpTitleRect;
  233.     Point menuPt;
  234.     MenuHandle popUpMenu;
  235.     char resStr[5];
  236.     WDPBRec paramBlock;
  237.     void MakeDownloadBold(DialogPtr);
  238.  
  239.     mainDlg = myDialog;
  240.     if (item == firstTime) {
  241.  
  242.         for (i=0; i<sizeof(itemTbl)/sizeof(struct itemTbl); i++) {
  243.             GetDItem(myDialog, itemTbl[i].itemNo, &itemType, &control, &box);
  244.             switch (itemType) {
  245.                 case ctrlItem+chkCtrl:
  246.                     for (j=0; j<6; j++) {
  247.                         if (cardItem[j] == itemTbl[i].itemNo) {
  248.                             if (slotMask & (1<<(j+9)))
  249.                                 HiliteControl((ControlHandle)control, 0);
  250.                             else
  251.                                 HiliteControl((ControlHandle)control, 255);
  252.                         }
  253.                     }
  254.                     if (*(Boolean *)itemTbl[i].itmPtr)
  255.                         SetCtlValue((ControlHandle)control, 1);
  256.                     else
  257.                         SetCtlValue((ControlHandle)control, 0);
  258.                     break;
  259.                 case ctrlItem+radCtrl:
  260.                     for (j=0; j<(itemTbl[i].itmPtr)->radPtr->radNoItems; j++) {
  261.                         GetDItem(myDialog, (itemTbl[i].itmPtr)->radPtr->radItems[j], &itemType, &control, &box);
  262.                         SetCtlValue((ControlHandle)control, 0);
  263.                     }
  264.                     GetDItem(myDialog, radListOption[option], &itemType, &control, &box);
  265.                     SetCtlValue((ControlHandle)control, 1);
  266.                     break;
  267.                 case statText:
  268.                     SetupResource(myDialog, resTypeStr);
  269.                     break;
  270.                 default:
  271.                     break;
  272.             }
  273.         }
  274.     }
  275.     else if (item == nullEvent) {
  276.         if (!hiliteDownload) {
  277.             MakeDownloadBold(myDialog);
  278.             hiliteDownload = TRUE;
  279.         }
  280.         else {
  281.             if (!rsrcPut) {
  282.                 SetupResource(myDialog, resTypeStr);
  283.                 if (errCode) {
  284.                     GetDItem(mainDlg, 21, &itemType, &control, &box);
  285.                     SetIText(control, errMsg[errCode]);
  286.                 }
  287.                 rsrcPut = TRUE;
  288.             }
  289.         }
  290.     }
  291.     else {
  292.         if (item == 1) {
  293.             for (i=0; i<sizeof(itemTbl)/sizeof(struct itemTbl); i++) {
  294.                 GetDItem(myDialog, itemTbl[i].itemNo, &itemType, &control, &box);
  295.                 if (itemType == editText) {
  296.                     getitext(control, (char *)itemTbl[i].itmPtr);
  297.                 }
  298.  
  299.             }
  300.             Cancel = FALSE;
  301.         }
  302.         else {
  303.  
  304.             GetDItem(mainDlg, 21, &itemType, &control, &box);
  305.             SetIText(control, "\p                    ");
  306.             
  307.             for (i=0; i<sizeof(itemTbl)/sizeof(struct itemTbl); i++) {
  308.                 if (item == itemTbl[i].itemNo) {
  309.                     GetDItem(myDialog, item, &itemType, &control, &box);
  310.                     switch (itemType) {
  311.                         case ctrlItem+chkCtrl:
  312.                             if (*(Boolean *)itemTbl[i].itmPtr = !*(Boolean *)itemTbl[i].itmPtr)
  313.                                 SetCtlValue((ControlHandle)control, 1);
  314.                             else
  315.                                 SetCtlValue((ControlHandle)control, 0);
  316.                             break;
  317.                         case ctrlItem+radCtrl:
  318.                             for (j=0; j<itemTbl[i].itmPtr->radPtr->radNoItems; j++) {
  319.                                 GetDItem(myDialog, itemTbl[i].itmPtr->radPtr->radItems[j], &itemType, &control, &box);
  320.                                 SetCtlValue((ControlHandle)control, 0);
  321.                             }
  322.                             GetDItem(myDialog, item, &itemType, &control, &box);
  323.                             SetCtlValue((ControlHandle)control, 1);
  324.                             *(itemTbl[i].itmPtr->radPtr->radValPtr) = (itemTbl[i].itmPtr)->riValue;
  325.                             break;
  326.                         case statText:
  327.                             menuID = UniqueID('MENU');
  328.                             popUpMenu = NewMenu(menuID, "\pResource Type");
  329.                             InsertMenu(popUpMenu, -1);
  330.                             resStr[0] = 4;
  331.                             codeNum = 1;
  332.                             
  333.                             paramBlock.ioCompletion = 0;
  334.                             paramBlock.ioNamePtr = 0;
  335.                             paramBlock.ioVRefNum = 0;
  336.                             paramBlock.ioWDProcID = 0;
  337.                             paramBlock.ioWDDirID = *(long *)CurDirStore;
  338.                             PBOpenWD(¶mBlock, FALSE);
  339.                             
  340.                             
  341.                             if ((refNum = OpenRFPerm(reply.fName, paramBlock.ioVRefNum, fsRdPerm)) != -1) {
  342.                                 noOfRsrcTypes = Count1Types();
  343.                                 if (noOfRsrcTypes > 32) {
  344.                                     GetDItem(mainDlg, 21, &itemType, &control, &box);
  345.                                     SetIText(control, "\pToo many Resources in this file");
  346.                                 }
  347.                                 else {
  348.                                     
  349.                                     for (j=1; j<=noOfRsrcTypes; j++) {
  350.                                         Get1IndType((ResType *)(&result), j);
  351.                                         if (result == *(long *)PrevRsrc)
  352.                                             codeNum = j;
  353.                                         resStr[1] = ((char *)&result)[0];    // Move resource to resource string
  354.                                         resStr[2] = ((char *)&result)[1];
  355.                                         resStr[3] = ((char *)&result)[2];
  356.                                         resStr[4] = ((char *)&result)[3];
  357.                                         AppendMenu(popUpMenu, resStr);
  358.                                     }
  359.                                     
  360.                                     GetDItem(myDialog, item-1, &itemType, &control, &popUpTitleRect);
  361.                                     InvertRect(&popUpTitleRect);
  362.                                     
  363.                                     GetDItem(myDialog, item, &itemType, &control, &box);
  364.                                     tempRect = box;
  365.                                     InsetRect(&tempRect, -4, -4);
  366.                                     EraseRect(&tempRect);
  367.                                     InsertMenu(popUpMenu, -1);
  368.                                     menuPt.v = box.top;
  369.                                     menuPt.h = box.left;
  370.                                     LocalToGlobal(&menuPt);
  371.     
  372.                                     result = PopUpMenuSelect(popUpMenu, menuPt.v, menuPt.h, codeNum);
  373.     
  374.                                     if (LoWord(result)) {
  375.  
  376.                                         /*  Process menu selection  */
  377.                                         
  378.                                         getitem(popUpMenu, LoWord(result), resTypeStr);
  379.                                         
  380.                                         SetupResource(myDialog, resTypeStr);
  381.                                         strcpy(PrevRsrc, resTypeStr);
  382.                                     }
  383.                                     else {
  384.                                         SetupResource(myDialog, PrevRsrc);
  385.                                     }
  386.                         
  387.                                     DeleteMenu(menuID);
  388.                                     
  389.                                     GetDItem(myDialog, item-1, &itemType, &control, &popUpTitleRect);
  390.                                     InvertRect(&popUpTitleRect);
  391.                                     
  392.                                 }
  393.                                 CloseResFile(refNum);
  394.                             }
  395.                             break;
  396.  
  397.                         default:
  398.                             break;
  399.                     }
  400.                 }
  401.             }
  402.         }
  403.     }
  404.     if (item == getCancel)
  405.         Cancel = TRUE;
  406.     return item;
  407. }
  408.  
  409. void MakeDownloadBold(myDialog)
  410. DialogPtr myDialog;
  411. {
  412.     GrafPtr savePort;
  413.     PenState savePnState;
  414.     short itemType;
  415.     Handle control;
  416.     Rect myRect;
  417.     GetDItem(myDialog, 1, &itemType, &control, &myRect);
  418.     GetPort(&savePort);
  419.     GetPenState(&savePnState);
  420.     SetPort(myDialog);
  421.     PenSize(3,3);
  422.     InsetRect(&myRect, -4, -4);
  423.     FrameRoundRect(&myRect, 16, 16);
  424.     SetPenState(&savePnState);
  425.     SetPort(savePort);
  426. }
  427.  
  428. long cnvhex(hexstr, hexlen)
  429. char hexstr[];
  430. int hexlen;
  431. {
  432.     long val;
  433.     char tmpstr[17];
  434.     if (hexlen>16) hexlen=16;
  435.     strncpy(tmpstr, hexstr, hexlen);
  436.     tmpstr[hexlen] = '\0';
  437.     sscanf(tmpstr, "%lx", &val);
  438.     return val;
  439. }
  440.  
  441. pascal void ProgProc(segSize)
  442. unsigned long segSize;
  443. {
  444.     short itemType;
  445.     char sizeStr[9];
  446.     Handle control;
  447.     Rect box;
  448.     GetDItem(mainDlg, 21, &itemType, &control, &box);
  449.     sprintf(sizeStr,"Downloading %1X with %08X bytes", slotNUM, segSize);
  450.     setitext(control, sizeStr);
  451.     return;
  452. }
  453.  
  454. void SetupResource(myDialog, resTypeStr)
  455. DialogPtr myDialog;
  456. char resTypeStr[5];
  457. {
  458.     PenState pnState;
  459.     short itemType;
  460.     Rect box, tempRect;
  461.     Handle control;
  462.  
  463.     GetDItem(myDialog, 23, &itemType, &control, &box);
  464.     tempRect = box;
  465. /*    EraseRect(&box);
  466.     FrameRect(&box); */
  467.     GetPenState(&pnState);
  468.     PenSize(1,1);
  469.     PenMode(patCopy);
  470.     InsetRect(&tempRect, -1, -1);
  471. /*    RGBForeColor((RGBColor *)RGBBlack);*/
  472.     FrameRect(&tempRect);
  473.     tempRect.bottom += 1;
  474.     tempRect.right += 1;
  475.     PenSize(1,1);
  476.     PenMode(patCopy);
  477. /*    RGBForeColor((RGBColor *)RGBBlack);
  478.     FrameRect(&tempRect); */
  479.     MoveTo(tempRect.left+1, box.bottom+1);
  480.     LineTo(box.right+1, box.bottom+1);
  481.     LineTo(box.right+1, box.top);
  482.     MoveTo(box.left+12, box.bottom-4);
  483.     drawstring(resTypeStr);
  484.     SetPenState(&pnState);
  485. }
  486.